#!/usr/bin/python
# -*- coding: UTF-8 -*-

################################################################################
#
# Copyright (c) 2020 openEuler.org, Inc. All Rights Reserved
#
################################################################################
"""

1. Process manual excel data;
2. Crawling the maintainer of the warehouse;
3. Initialize the database file;

Authors: xiaojianghui
Date:    10/22/2020 11:01 AM
"""
import flask
from flask import request
from controller import timertaskcontroller
from tabletask import supplement_cve
from gevent import pywsgi
import threading

app = flask.Flask(__name__)


@app.route('/pulls/cve/info', methods=['post'])
def pull_cve_info():
    """
    1. Receive messages
    2. Grab cve data
    """
    cve_num = request.json.get("CveNum")
    if cve_num and len(cve_num) > 0:
        cve_num = str(cve_num).strip()
        ret_value = supplement_cve.pull_cve(cve_num)
        if ret_value == 1:
            return {
                "code": 200,
                "message": "success"
            }
        else:
            return {
                "code": 404,
                "message": "Data does not exist"
            }
    else:
        return {
            "code": 405,
            "message": "Parameter error"
        }


def api_proc():
    """
    1. api thread processing function
    """
    server = pywsgi.WSGIServer(('0.0.0.0', 8080), app)
    server.serve_forever()


if __name__ == '__main__':
    print("Start a scheduled task...")
    # thread_timed_task = threading.Thread(target=timertaskcontroller.timertask)
    # thread_timed_task.start()
    # thread_timed_task.join()
    # thread_api_proc = threading.Thread(target=api_proc)
    # thread_api_proc.start()
    # thread_api_proc.join()
